using System.Collections.Generic;
using System.Linq;
using System.Text;
using Guesshoo.TrainExpress.Extensions;

namespace Guesshoo.TrainExpress
{
	public class Route : IRoute
	{
		private readonly IEnumerable<StationLink> _stationLinks;
		private readonly int _travelTime;

		public Route(IEnumerable<StationLink> stationLinks, int travelTime) {
			_stationLinks = stationLinks;
			_travelTime = travelTime;
		}

		public override string ToString() {
			return _stationLinks == null
			       	? "<No Route Found>"
			       	:
			       		"{0}  [{1}]".FormatWith(OutputRoute(), TravelTime);
		}

		public IEnumerable<StationLink> StationLinks {
			get { return _stationLinks; }
		}

		internal string OutputRoute() {
			return OutputRoute(_stationLinks);
		}

		internal string OutputRoute(IEnumerable<StationLink> stationLinks) {
			var sb = new StringBuilder();
			var routeList = stationLinks.ToList();

			routeList.ForEach(x => sb.AppendFormat("{0}->", x.Source));
			sb.AppendFormat("{0}", routeList.Last().Target);

			return sb.ToString();
		}

		public virtual int TravelTime {
			get { return _travelTime; }
		}
	}
}